<?php
	
	require_once ($_SERVER['DOCUMENT_ROOT']."/Auditing/business/InsertRule.php");
	require_once ($_SERVER['DOCUMENT_ROOT']."/Auditing/business/UpdateRule.php");
	require_once ($_SERVER['DOCUMENT_ROOT']."/Auditing/business/RetrieveRules.php");
	require_once ($_SERVER['DOCUMENT_ROOT']."/Auditing/business/RetrieveComboData.php");
	require_once ($_SERVER['DOCUMENT_ROOT']."/Auditing/business/util/Utility.php");
	require_once ($_SERVER['DOCUMENT_ROOT']."/Auditing/business/EngineParser.php");
	require_once ($_SERVER['DOCUMENT_ROOT']."/Auditing/business/LoginPost.php");
	require_once ($_SERVER['DOCUMENT_ROOT']."/Auditing/business/UserController.php");
	
	$param =  $_GET["param"];
	
	// Rule Insertion Controller
	if($param=="rules"){
		
		$operation= $_GET["operation"];
		
		$rule_name = $_POST["txt_rule_name"];
		$flag = $_POST["combo_flag"];
		$count = $_POST["criteria_counter"];
		$field = $_POST["combo_field"];
		$logic = $_POST["combo_logic"];
		$text_value = $_POST["text_value"];
		
		if($operation=="insert"){
			$insertObj = new InsertRule($rule_name,$flag,$field,$logic,$text_value); 
			$val = $insertObj->saveRule();
		}
		else if($operation=="update"){
			$rule_meta_data_id =  $_POST["rule_meta_data_id"];
			
			$updateObj = new UpdateRule($rule_name,$flag,$field,$logic,$text_value,$rule_meta_data_id); 
			$val = $updateObj->saveRule();			
		}
		
		//echo $_SERVER['PHP_SELF'];
	    if($val==1){
			header ("Location: /Auditing/view/Rule/RuleCreate.php?insert=true");
	    }
	    else if($val==0){
	    	header ("Location: /Auditing/view/Rule/RuleCreate.php?insert=false");
	    }
		
	}
	// Retrieve Rule Controller
	else if($param=="retrieveRule"){
	//	echo("testing");
		$rule_meta_data_id = $_POST["combo_rule_meta"];
				
		$rulesObj = new RetrieveRules(); 
		$val = $rulesObj->getData("",$rule_meta_data_id);
	//	$str = "Location: /Auditing/view/Rule/ViewRule.php?values=".$val;
		header ("Location: /Auditing/view/Rule/ViewRule.php?values=yes");
		echo $val;
	    		
	}
	// Reporting Engine Controller
	else if($param=="reportEngine"){

		
		
		$option =$_GET["option"];
		$tblNames = $_POST["tblName"];
				
		if($option=="tables"){
			
		$arr = getTokenizedArray($tblNames,",");
		
			$objCombo = new RetrieveComboData();
			echo $objCombo->getFieldNames($arr);
		}
		else if($option=="report"){
			
			$strQuery = $_POST["strQuery"];
			$counter = $_POST["counter"];
			
			echo getReportData($strQuery,$counter);
			
		}
		
	}
	// Login and Logout Controller
		else if($param=="loginPost"){
			
			$option = $_GET['option'];
			
			if($option=="login"){
				
				$userName = $_POST['username'];
				$pass = $_POST['password'];
			
				if (!isset($userName) || !isset($pass)) {
					
					header( "Location: /Auditing/Login.php" );
				}
				//check that the form fields are not empty, and redirect back to the login page if they are
				elseif (empty($userName) || empty($pass)) {
					header( "Location: /Auditing/Login.php" );
				}
				else{
						
					$check = checkLogin($userName,$pass);
					
					if($check==1){

						header( "Location: /Auditing/view/Rule/RuleCreate.php" );
					}else if($check==0){
						header( "Location: /Auditing/Login.php?login=false");
					}
				}			
			}else if($option=="logout"){
				
				if(logOut()){
					header( "Location: /Auditing/Login.php");
				}else {
					echo "There was some problem";
				}
			}			
		}

		//User Creation and Use View Controller

		else if($param=="userPost"){
			
			$option = $_GET['operation'];
			$userObj = new UserController();
			
			$user_name = $_POST['text_user_name'];
			$password = $_POST['text_password'];
			$pages = $_POST['combo_page'];
			
			if($option=="insert"){

								
				$val = $userObj->createUser($user_name,$password,$pages);
					
			}
			if($option=="update"){
				
				$val = $userObj->updateUsers($user_name,$password,$pages,$_POST['user_id']);							
			}
			
			if($val==1){
				header ("Location: /Auditing/view/admin/UserMgmt.php?insert=true");
		    }
		    else if($val==0){
		    	header ("Location: /Auditing/view/admin/UserMgmt.php?insert=false");
		    }
		    
		}
?>
